package com.friend.base.controller.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.friend.base.service.base.DictService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-10-11
 */
@Api(tags = {"字典表控制类"})
@RestController
@RequestMapping(value = "/dict", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @GetMapping("/getDictByType")
    @ApiOperation(value = "查询字典值", notes = "根据字典类型查询字典值", httpMethod = "GET")
    public Result getDictByType(@RequestParam("type") Integer type, @RequestParam("parentId") Integer parentId){
        try {
            List<Dict> dicts = dictService.list(new QueryWrapper<Dict>().eq("type", type).eq("parent_id", parentId));
            if (CollectionUtils.isEmpty(dicts)){
                return this.createFailedResult("字典值查询失败");
            }
            return this.createSuccessResult(dicts.toArray());
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("字典值查询失败");
        }
    }
}

